<?php

namespace app\uapi\controller;

use app\admin\model\KnscyActivity;
use app\admin\model\KnscyActivityLogs;
use app\common\controller\ApiController;
use think\facade\Db;

class Activity extends  ApiController
{
    public static  $needLoginAction = [
        'getDetailsById', //报名详情页面
        'addActivityLog'
    ];
    public function getActivityList()
    {
        $limit = $this->request->post('limit', 10);
        $page = $this->request->post('page', 1);


        $List = KnscyActivity::where('status', 1)
            //->whereTime('start_time', '<=', date('Y-m-d H:i:s'))
            //->whereTime('end_time', '>=', date('Y-m-d H:i:s'))
            ->order(Db::raw('sort=0,sort asc,id desc'))
            ->paginate(
                [
                    'list_rows' => $limit,
                    'page' => $page,
                ]
            )->each(function ($item){
                $item['cover'] = get_full_url($item->cover);
                $item['end_time_txt'] = '已结束';
                if(strtotime($item->start_time) < time() && strtotime($item->end_time) >= time()){
                    $item['end_time_txt'] = date('Y-m-d H:i',strtotime($item->end_time)) .'截止';
                }

            });

        $this->success('success', $List);

    }

    /**
     * 通过单活动详情一片内容
     * @return void
     */
    public function getDetailsById()
    {
        $id = $this->request->post('id');

        $data = KnscyActivity::where('status', 1)
            ->where('id', $id)
            ->order(Db::raw('sort=0,sort asc,id desc'))
            ->find();
        if (empty($data)) {
            $this->success('暂无数据');
        }
        //查询是否报名过
        $logs = KnscyActivityLogs::where('activity_id', $id)
            ->order(Db::raw('sort=0,sort asc,id desc'))
            ->where('user_id', $this->user_id)
            ->find();
        $data['has_agent'] = 0;
        if(!empty($logs)){
            $data['has_agent'] = 1; //已经报名过
        }

        $data->content = img_alt($data->content);

        $this->success('success', $data);
    }

    /**
     * 报名活动
     * @return void
     */
    public function addActivityLog()
    {
        $id = $this->request->post('id');

        $info = KnscyActivity::where('id', $id)->find();
        if (empty($info)) {
            $this->error('活动信息有误',[],1);
        }
        //检查是否有记录
        $agent = KnscyActivityLogs::where('activity_id', $id)
            ->where('user_id', $this->user_id)->find();
        if (!empty($agent)) {
            $this->error('已经报名过了',[],1);
        }
        //是否領取完
        $total = KnscyActivityLogs::where('activity_id', $id)->count();
        if($total>= $info['total_nums']){
            $this->error('达到报名人数上限',[],1);
        }
        $data = [
            'activity_id' => $id,
            'user_id' => $this->user_id,
            'tag' =>  $total +1 , //序号
            'content' => $info['title'],
            'status' => 1, //审核状态
        ];
        $agent = KnscyActivityLogs::create($data);

        $this->success('success', $agent);
    }

    /**
     * 我报名的活动
     * @return
     */
    public function myActivityLogs()
    {
        $limit = $this->request->post('limit', 10);
        $page = $this->request->post('page', 1);

        $list = KnscyActivityLogs::where('user_id', $this->user_id)

            ->paginate(
                [
                    'list_rows' => $limit,
                    'page' => $page,
                ]
            )->each(function($item){
                $item['create_time_label'] = date('Y-m-d H:i', strtotime($item['create_time']));
            });
        $this->success('success', $list);
    }

}